package com.momo.service.sysSetting;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Multimap;
import com.momo.common.req.sysmain.RedisUser;
import com.momo.common.res.sysSetting.DeptLevelRes;
import com.momo.common.util.LevelUtil;
import com.momo.mapper.dao.DeptDAO;
import com.momo.mapper.dataobject.DeptDO;
import com.momo.mapper.mapper.sysSetting.DeptMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by MOMO on 2019/2/18.
 * 部门树
 */
@Service
@Slf4j
public class SysDeptTreeService {

    @Autowired
    private DeptMapper deptMapper;

    public List<DeptLevelRes> deptTree(RedisUser redisUser) {
        //查询当前登录用户所在的第三方的所有部门
        List<DeptDO> deptList = deptMapper.getAllDept(redisUser.getGroupId());

        List<DeptLevelRes> dtoList = Lists.newArrayList();
        for (DeptDO dept : deptList) {
            //DO转为前端需要的VO
            DeptLevelRes dto = DeptLevelRes.adapt(dept);
            dtoList.add(dto);
        }
        return deptListToTree(dtoList);
    }
    private List<DeptLevelRes> deptListToTree(List<DeptLevelRes> deptLevelList) {
        if (CollectionUtils.isEmpty(deptLevelList)) {
            return Lists.newArrayList();
        }
        // level -> [dept1, dept2, ...] Map<String, List<Object>>
        Multimap<String, DeptLevelRes> levelDeptMap = ArrayListMultimap.create();
        List<DeptLevelRes> rootList = Lists.newArrayList();

        for (DeptLevelRes dto : deptLevelList) {
            levelDeptMap.put(dto.getSysDeptLevel(), dto);
            if (LevelUtil.ROOT.equals(dto.getSysDeptLevel())) {
                rootList.add(dto);
            }
        }
        // 按照seq从小到大排序
        Collections.sort(rootList, new Comparator<DeptLevelRes>() {
            @Override
            public int compare(DeptLevelRes o1, DeptLevelRes o2) {
                return o1.getSysDeptSeq() - o2.getSysDeptSeq();
            }
        });
        // 递归生成树
        transformDeptTree(rootList, LevelUtil.ROOT, levelDeptMap);
        return rootList;
    }
    // level:0, 0, all 0->0.1,0.2
    // level:0.1
    // level:0.2
    private void transformDeptTree(List<DeptLevelRes> deptLevelList, String level, Multimap<String, DeptLevelRes> levelDeptMap) {
        for (int i = 0; i < deptLevelList.size(); i++) {
            // 遍历该层的每个元素
            DeptLevelRes deptLevelDto = deptLevelList.get(i);
            // 处理当前层级的数据
            String nextLevel = LevelUtil.calculateLevel(level, deptLevelDto.getId());
            // 处理下一层
            List<DeptLevelRes> tempDeptList = (List<DeptLevelRes>) levelDeptMap.get(nextLevel);
            if (CollectionUtils.isNotEmpty(tempDeptList)) {
                // 排序
                Collections.sort(tempDeptList, deptSeqComparator);
                // 设置下一层部门
                deptLevelDto.setDeptList(tempDeptList);
                // 进入到下一层处理
                transformDeptTree(tempDeptList, nextLevel, levelDeptMap);
            }
        }
    }
    private Comparator<DeptLevelRes> deptSeqComparator = new Comparator<DeptLevelRes>() {
        @Override
        public int compare(DeptLevelRes o1, DeptLevelRes o2) {
            return o1.getSysDeptSeq() - o2.getSysDeptSeq();
        }
    };
}
